package com.team.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.Exception.ServiceException;
import com.team.commons.dto.ProductDto;
import com.team.commons.utils.ColaBeanUtils;
import com.team.commons.vo.ProductDetailsVo;
import com.team.commons.vo.ProductListVo;
import com.team.commons.vo.ProductTypeVo;
import com.team.commons.vo.ProductVo;
import com.team.entity.Product;
import com.team.mapper.ProductMapper;
import com.team.result.StatusCode;
import com.team.service.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    ProductMapper productMapper;
    @Resource
    ProductService productService;

    @Override
    public ProductListVo selectList(Integer current, Integer state, String type) {
        ProductListVo listVo = new ProductListVo();
        try {
            Page<Product> page = new Page<>(current,10);
            QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq(Product.COL_STATE,state).eq(Product.COL_PRODUCT_TYPE,type);
            List<Product> pages = productMapper.selectPage(page, queryWrapper).getRecords();
            List<ProductVo> list = ColaBeanUtils.copyListProperties(pages, ProductVo::new);
            ProductTypeVo sum = productService.selectProductSum();
            listVo.setProductVos(list);
            listVo.setProductTypeVos(sum);
        }catch (Exception e){
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return listVo;
    }

    @Override
    public List<ProductVo> selectListKey(Integer current,String key) {
        List<ProductVo> list = new ArrayList<>();
        try {
            Page<Product> page = new Page<>(current,10);
            QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
            queryWrapper.like(Product.COL_PATH_NAME,key);
            List<Product> pages = productMapper.selectPage(page, queryWrapper).getRecords();
            list = ColaBeanUtils.copyListProperties(pages, ProductVo::new);
        }catch (Exception e){
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return list;
    }

    @Override
    public ProductDetailsVo selectProductDetails(Long productId) {
        ProductDetailsVo vo = new ProductDetailsVo();
        try {
            Product product = productMapper.selectById(productId);
            if(product.getId()==null){
                ProductDto productDto = new ProductDto();
                productDto.setId(productId);
                Integer integer = productService.insertProduct(productDto);
                if(integer<1){
                    throw new ServiceException(StatusCode.INTER_ERROR);
                }
            }
            ColaBeanUtils.copyProperties(product,vo);
        }catch (Exception e){
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return vo;
    }

    @Override
    public Integer insertProduct(ProductDto productDto) {
        int insert=0;
        try {
            Product product = new Product();
            ColaBeanUtils.copyProperties(productDto,product);
            insert = productMapper.insert(product);
        }catch (Exception e){
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return insert;
    }

    @Override
    public Integer updateProduct(ProductDetailsVo productDetailsVo) {
        int insert=0;
        try {
        Product product = new Product();
        ColaBeanUtils.copyProperties(productDetailsVo,product);
        insert = productMapper.updateById(product);
        }catch (Exception e){
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return insert;
    }

    @Override
    public Integer updateStateProduct(List<Long> productIdList, Integer state) {
        AtomicInteger update= new AtomicInteger();
        try {
            productIdList.forEach(productId->{
                Product product = productMapper.selectById(productId);
                product.setState(state);
                int i = productMapper.updateById(product);
                if(i<1){
                    throw new ServiceException(StatusCode.UPDATE_ERROR);
                }
            });
        }catch (Exception e){
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return update.get();
    }

    @Override
    public ProductTypeVo selectProductSum() {
        ProductTypeVo typeVo = new ProductTypeVo();
        try {
            Integer count = productMapper.selectCount(new QueryWrapper<>());
            Integer toProduct = productMapper.selectCount(new QueryWrapper<Product>().eq(Product.COL_PRODUCT_TYPE, 1));
            Integer stayProduct = productMapper.selectCount(new QueryWrapper<Product>().eq(Product.COL_PRODUCT_TYPE, 2));
            Integer repertory = productMapper.selectCount(new QueryWrapper<Product>().eq(Product.COL_PRODUCT_TYPE, 3));
            Integer drafts = productMapper.selectCount(new QueryWrapper<Product>().eq(Product.COL_PRODUCT_TYPE, 4));
            typeVo.setAllProduct(count);
            typeVo.setToProduct(toProduct);
            typeVo.setStayProduct(stayProduct);
            typeVo.setDrafts(drafts);
            typeVo.setRepertory(repertory);
        }catch (Exception e){
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return typeVo;
    }
}
